<?php
namespace app\admin\controller;
use think\Db;

class User
{
        public function __construct(){
		Admin_auth();
	}
	
    public function index()
    {
        $where = [];
        $aid = request()->request('aid');
        if(!empty($aid)){
            $where['aid'] = $aid;
        }
		$pages = pages('user',$where,input('get.page'));
	    
		$data = Db::table("user")->whereIn('aid',session('aid'))->where($where)->limit($pages['limit'])->order('id desc')->select();
		//所属地和余额
        return view('',['data' => $data,'pages' => $pages['pages']]);
    }
	
	public function edit()
    {
		$id = (int)request()->get('id');
		if(request()->isPost()){
			$data = request()->post();
			if(Db::table("user")->where(['phone' => $data['phone']])->where('id','<>',$id)->find()){
				return error(lang('tip')['re_phone']);
			}
			if($id){
				if(empty($data['password'])){
					unset($data['password']);
				}else{
					$data['password'] = md5($data['password']);
				}
				Db::table("user")->where(['id' => $id])->update($data);
			}else{
				$data['regtime'] = time();
				$data['password'] = md5($data['password']);
				//$data['token'] = md5($data['phone'].$data['password']);
				$data['introduce']="";
				$id = Db::table("user")->insertGetId($data);
			}
		}
        $data = Db::table("user")->where(['id' => $id])->find();
        return view('',['data'  => $data]);
    }
	
	public function del()
    {
		$id = input("get.id")?input("get.id"):0;
		$page = input("get.page");
		Db::table("user")->whereIn('aid',session('aid'))->delete($id);
		redirect(url('/admin/user').'?page='.$page);
		exit;
    }

    public function opinion()
    {
        $pages = pages('opinion','',input('get.page'));
        $data = Db::table('opinion')->limit($pages['limit'])->order('time desc')->select();
        return view('',['data'=>$data,'pages' => $pages['pages']]);
    }

    public function flag()
    {
        $data = Db::table('flag_card')->select();
        return view('',['data'=>$data]);
    }

    public function upload()
    {
        $config = Db::table('material_config')->where('id',1)->find();
        config([
            'UPLOAD_SITEIMG_QINIU'=>[
                'maxSize'          => 5 * 1024 * 1024,//文件大小
                'rootPath'         => './',
                'saveName'         => ['uniqid', ''],
                'driver'           => 'Qiniu',
                'driverConfig' => [
                    'secretKey' => $config['secretKey'],
                    'accessKey' => $config['accessKey'],
                    'domain'    => $config['domain'],
                    'bucket'    => $config['bucket'],
                ],
            ],
            'file_size'=>$config['file_size']
        ]);
        $setting=config('UPLOAD_SITEIMG_QINIU');
        $Upload = new \think\Upload($setting);
        $infos = $Upload->upload($_FILES);
        return $infos;
    }
	
    public function setting()
    {
		if(request()->isPost()){
			$data = input("post.");
			Db::table("user_setting")->where(['id'=>1])->update($data);
        }
		$data = Db::table("user_setting")->where(['id'=>1])->find();
        return view('',['data' => $data]);
    }
}
